package com.jyu.apts.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.jyu.apts.dto.QRCodeTemplateDto;
import com.jyu.apts.dto.ResultDto;
import com.jyu.apts.dto.TableDto;
import com.jyu.apts.dto.UserDto;
import com.jyu.apts.entity.QRCodeTemplate;
import com.jyu.apts.entity.User;
import com.jyu.apts.service.QRCodeTemplateService;
import com.jyu.apts.service.UserService;

@RestController
public class QRCodeTemplateController {

	@Autowired
	QRCodeTemplateService qrCodeTemplateService;
	@Autowired
	UserService userService;
	
	@RequiresPermissions("qrCodeTemplate_html")
	@RequestMapping("/qrCodeTemplate_html")
	public ModelAndView qrCodeTemplate_html() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/qrCodeTemplate.html");
		return mv;
	}
	
	@RequiresPermissions("qrCodeTemplate_html")
	@RequestMapping("/addQRCodeTemplate")
	public ResultDto addQRCodeTemplate(String name,String[] dataHeads,String description,Integer visibility) {
		StringBuilder sb = new StringBuilder("");
		if(dataHeads == null || dataHeads.length == 0) {
			sb.append("#");
		}
		for(String dataHead : dataHeads) {
			sb.append("#");
			sb.append(dataHead);
		}
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		QRCodeTemplate qrCodeTemplate = new QRCodeTemplate();
		qrCodeTemplate.setName(name);
		qrCodeTemplate.setAuthor(user);
		qrCodeTemplate.setDataHeads(sb.toString().substring(1));
		qrCodeTemplate.setDescription(description);
		qrCodeTemplate.setVisibility(visibility);
		qrCodeTemplateService.save(qrCodeTemplate);
		return new ResultDto("添加成功",null,0);
	}
	
	@RequiresPermissions("qrCodeTemplate_html")
	@RequestMapping("/upQRCodeTemplate")
	public ResultDto upQRCodeTemplate(long id,String[] dataHeads,String name,String description,Integer visibility) {
		QRCodeTemplate qrCodeTemplate = qrCodeTemplateService.getOne(id);
		if(qrCodeTemplate == null) {
			return new ResultDto("二维码模板不存在!",null,2);
		}
		StringBuilder sb = new StringBuilder("");
		if(dataHeads == null || dataHeads.length == 0) {
			sb.append("#");
		}
		for(String dataHead : dataHeads) {
			sb.append("#");
			sb.append(dataHead);
		}
		qrCodeTemplate.setDescription(description);
		qrCodeTemplate.setName(name);
		qrCodeTemplate.setDataHeads(sb.toString().substring(1));
		qrCodeTemplate.setVisibility(visibility);
		qrCodeTemplateService.save(qrCodeTemplate);
		return new ResultDto("更新成功",null,0);
	}
	
	@RequiresPermissions("qrCodeTemplate_html")
	@RequestMapping("/delQRCodeTemplate")
	public ResultDto delQRCodeTemplate(String ids) {
		String[] idArray = ids.split(",");
		List<QRCodeTemplate> qrCodeTemplates = new ArrayList<QRCodeTemplate>();
		for(String id : idArray) {
			QRCodeTemplate qrCodeTemplate = new QRCodeTemplate();
			qrCodeTemplate.setId(new Long(id));
			qrCodeTemplates.add(qrCodeTemplate);
		}
		qrCodeTemplateService.delete(qrCodeTemplates);
		return new ResultDto("删除成功",null,0);
	}
	
	@RequiresPermissions("qrCodeTemplate_html")
	@RequestMapping("/getQRCodeTemplatePage")
	public TableDto<QRCodeTemplateDto> getQRCodeTemplatePage(Integer pageNumber,Integer pageSize,Long organizationId,String search) {
		Page<QRCodeTemplate> pageResult = qrCodeTemplateService.getQRCodeTemplatePage(pageNumber, pageSize, organizationId, search);
		List<QRCodeTemplate> qrCodeTemplates = pageResult.getContent();
		List<QRCodeTemplateDto> qrCodeTemplateDtos = new ArrayList<QRCodeTemplateDto>();
		qrCodeTemplates.forEach(qrCodeTemplate->qrCodeTemplateDtos.add(new QRCodeTemplateDto().convert(qrCodeTemplate)));
		return new TableDto<QRCodeTemplateDto>(qrCodeTemplateDtos,pageResult.getTotalElements());
	}
	
	@RequiresPermissions("qrCodeTemplate_html")
	@RequestMapping("/getAllQRCodeTemplateByOrganization")
	public List<QRCodeTemplateDto> getAllQRCodeTemplateByOrganization(long organizationId) {
		List<QRCodeTemplateDto> qrCodeTemplateDtos = new ArrayList<QRCodeTemplateDto>();
		List<QRCodeTemplate> qrCodeTemplates = qrCodeTemplateService.findAllByOrganization(organizationId);
		qrCodeTemplates.forEach(qrCodeTemplate->qrCodeTemplateDtos.add(new QRCodeTemplateDto().convert(qrCodeTemplate)));
		return qrCodeTemplateDtos;
	}
	
}
